/* 
1 组件-绑定点击事件
2 dispatch action 
3 reducer中 判断action 类型 业务处理 


1 todo reducer 先定义action 
2  odo reducer 业务处理好
3 组件-绑定点击事件

 */

// 1 初始化数据
const initialState = {
  list: [
    { id: 1, task: "你吃饭了吗", isDone: false },
    { id: 2, task: "你面试了吗", isDone: true },
    { id: 3, task: "你转正了吗", isDone: false },
  ],
};

// action 根据id来删除数据
// 联合类型
export type ActionType =
  | {
      type: "todo/delById";
      payload: number;
    }
  | {
      type: "todo/clearDone";
    };
// 想象一下 接收 根据id删除元素的 action
//  { type:"todo/delById",payload:2 }

// 不对action作结构
// 对 reducer函数 添加返回类型的约束
export default (
  state = initialState,
  action: ActionType
): typeof initialState => {
  switch (action.type) {
    case "todo/delById":
      return {
        ...state,
        list: state.list.filter((v) => v.id !== action.payload),
      };
    case "todo/clearDone":
      return {
        ...state,
        list: state.list.filter((v) => !v.isDone),
        // list: "",
      };
    default:
      return state;
  }
};
